package com.shinhye.demoproject.view;

import android.content.Context;
import android.graphics.Point;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.shinhye.demoproject.R;

/**
 * @author wl
 * @since 2016/08/09 10:00
 */
public class DraggableView extends LinearLayout {

    private ViewDragHelper dragger;
    private Point startPoint = new Point();

    public DraggableView(Context context) {
        super(context);

        init();
    }

    public DraggableView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    private void init() {
        dragger = ViewDragHelper.create(this, 1f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                // 哪些可以接收 drag操作
                return child.getId() != R.id.edge;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                // 这个child在水平方向可以移动的范围。
                int leftBound = getPaddingLeft();
                int rightBound = getWidth() - child.getWidth() - getPaddingRight();

                //表示只在这个parent的范围内水平移动child(包含padding)
                return Math.min(Math.max(left, leftBound), rightBound);
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                // 这个child在垂直方向可以移动的范围。
                int topBound = getPaddingTop();
                int bottomBound = getHeight() - child.getHeight() - getPaddingBottom();

                //表示只在这个parent的范围内垂直移动child(包含padding)
                return Math.min(Math.max(top, topBound), bottomBound);
            }

            //当某一个child在drag后，手指释放了
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                if(releasedChild.getId() == R.id.release) {
                    int newLeft = startPoint.x + releasedChild.getWidth();
                    int newTop = startPoint.y;
//                    dragger.smoothSlideViewTo(releasedChild, newLeft, newTop);
                    // 已捕获drag的child，设置其滑动到某一个位置
                    dragger.settleCapturedViewAt(newLeft, newTop);
//                    dragger.flingCapturedView(newLeft, newTop, newLeft + 200, newTop + 200);
                    invalidate();
                }
            }

            @Override
            public int getViewHorizontalDragRange(View child) {
                return 1;
            }

            //当边界drag 开始时
            @Override
            public void onEdgeDragStarted(int edgeFlags, int pointerId) {
                // 将drag的操作映射到某一个child上
                dragger.captureChildView(findViewById(R.id.edge), pointerId);
            }
        });

        // 要启用边界检查，必须调用此方法
        dragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.e("dragger", "onLayout");
        super.onLayout(changed, l, t, r, b);
        startPoint.x = l + getPaddingLeft();
        startPoint.y = t + getPaddingTop();
    }

    @Override
    public void computeScroll() {
        if(dragger.continueSettling(true)) {
            invalidate();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //必须这样调用检查是否打断
        return dragger.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //调用这个方法，才能让parent启用drag捕获
        dragger.processTouchEvent(event);
        return super.onTouchEvent(event);
    }
}
